import http from '@/utils/request'

/**
 * 获取所有科研项目
 * @returns {Promise}
 */
export function getResearchProjects() {
    return http({
        url: '/api/research-projects',
        method: 'get'
    })
}

/**
 * 获取当前用户的科研项目
 * @param {number} userId - 用户ID
 * @returns {Promise}
 */
export function getMyResearchProjects(userId) {
    return http({
        url: `/api/research-projects/my?userId=${userId}`,
        method: 'get'
    })
}

/**
 * 根据状态获取科研项目
 * @param {string} status - 项目状态
 * @returns {Promise}
 */
export function getResearchProjectsByStatus(status) {
    return http({
        url: `/api/research-projects/by-status/${status}`,
        method: 'get'
    })
}

/**
 * 获取待审核的科研项目
 * @returns {Promise}
 */
export function getPendingReviewResearchProjects() {
    return http({
        url: '/api/research-projects/pending-review',
        method: 'get'
    })
}

/**
 * 获取科研项目详情
 * @param {number} id - 项目ID
 * @returns {Promise}
 */
export function getResearchProjectById(id) {
    return http({
        url: `/api/research-projects/${id}`,
        method: 'get'
    })
}

/**
 * 创建科研项目
 * @param {FormData} formData - 项目数据（包含文件）
 * @returns {Promise}
 */
export function createResearchProject(formData) {
    return http({
        url: '/api/research-projects',
        method: 'post',
        data: formData
    })
}

/**
 * 更新科研项目（重新提交）
 * @param {number} id - 项目ID
 * @param {FormData} formData - 更新的项目数据
 * @returns {Promise}
 */
export function updateResearchProject(id, formData) {
    return http({
        url: `/api/research-projects/${id}/resubmit`,
        method: 'post',
        data: formData
    })
}

/**
 * 删除科研项目
 * @param {number} id - 项目ID
 * @returns {Promise}
 */
export function deleteResearchProject(id) {
    return http({
        url: `/api/research-projects/${id}`,
        method: 'delete'
    })
}

/**
 * 审核科研项目
 * @param {number} id - 项目ID
 * @param {Object} reviewData - 审核数据
 * @returns {Promise}
 */
export function reviewResearchProject(id, reviewData) {
    return http({
        url: `/api/research-projects/${id}/review`,
        method: 'post',
        data: reviewData
    })
}



/**
 * 开始执行项目
 * @param {number} id - 项目ID
 * @returns {Promise}
 */
export function startProjectExecution(id) {
    return http({
        url: `/api/research-projects/${id}/start-execution`,
        method: 'post'
    })
}

/**
 * 提交结题材料
 * @param {number} id - 项目ID
 * @param {FormData} formData - 结题材料数据
 * @returns {Promise}
 */
export function submitFinishMaterials(id, formData) {
    return http({
        url: `/api/research-projects/${id}/finish`,
        method: 'post',
        data: formData
    })
}

/**
 * 提交结题材料 (别名函数，与组件中的引用保持一致)
 * @param {number} id - 项目ID
 * @param {Object} formData - 结题材料数据
 * @returns {Promise}
 */
export function submitResearchFinish(id, formData) {
    return http({
        url: `/api/research-projects/${id}/finish`,
        method: 'post',
        data: formData
    })
}

/**
 * 根据项目类型查询
 * @param {string} type - 项目类型
 * @returns {Promise}
 */
export function getResearchProjectsByType(type) {
    return http({
        url: `/api/research-projects/by-type/${type}`,
        method: 'get'
    })
}

/**
 * 根据学科分类查询
 * @param {string} subject - 学科分类
 * @returns {Promise}
 */
export function getResearchProjectsBySubject(subject) {
    return http({
        url: `/api/research-projects/by-subject/${subject}`,
        method: 'get'
    })
}

/**
 * 根据项目级别查询
 * @param {string} level - 项目级别
 * @returns {Promise}
 */
export function getResearchProjectsByLevel(level) {
    return http({
        url: `/api/research-projects/by-level/${level}`,
        method: 'get'
    })
}

/**
 * 搜索科研项目
 * @param {string} keyword - 搜索关键词
 * @returns {Promise}
 */
export function searchResearchProjects(keyword) {
    return http({
        url: '/api/research-projects/search',
        method: 'get',
        params: { keyword }
    })
}

/**
 * 下载附件
 * @param {number} id - 附件ID
 * @param {string} filename - 文件名
 * @returns {Promise}
 */
export function downloadResearchAttachment(id, filename) {
    return http({
        url: `/api/attachments/${id}/download`,
        method: 'get',
        responseType: 'blob'
    }).then(response => {
        const url = window.URL.createObjectURL(new Blob([response]))
        const link = document.createElement('a')
        link.href = url
        link.setAttribute('download', filename)
        document.body.appendChild(link)
        link.click()
        document.body.removeChild(link)
        window.URL.revokeObjectURL(url)
    })
}
